<!DOCTYPE html>
<html lang="en">

<head>
	<title>three.js / editor</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
	<!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-09-11 -->
	<meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-09-11" content="AqhFUYKxq/d+E8CDT0fuYRCg8TvlTP52x0Jv7I9t27sLhR30LmcahBRfSwzP89ukjs2+ia99VrrLoRyaFAwJVA0AAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUzNjYyNDAwMH0=">
	<!-- Origin Trial Token, feature = WebXR Device API (For Chrome M69+), origin = https://threejs.org, expires = 2018-12-02 -->
	<meta http-equiv="origin-trial" data-feature="WebXR Device API (For Chrome M69+)" data-expires="2018-12-02" content="Ah46myef4Ax/+fcLtkeotXmIqnvun4lg4bYbHVttuJvbsWiE4oacrvroId7hbCEb4/byxFHIO6+uwq4pwr6RfQkAAABTeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZU02OSIsImV4cGlyeSI6MTU0Mzc1ODIyNn0=">
	<!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-12-02 -->
	<meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-12-02" content="AqI9LIanbGxr/HoTOsYCUNxG82Vy94NZbjhv83R+bF+5NX2jiZOaf7ny+mFoTUP5wrWpYlPjQ6+HeVas9f1lRwYAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTQzNzU4MjI2fQ==">
</head>

<body ontouchstart="">
	<!-- editor样式 -->
	<link href="css/main.css" rel="stylesheet" />
	<link id="theme" href="css/light.css" rel="stylesheet" />

	<!-- 引用基本的three.js库  -->
	<script src="../build/three.js"></script>
	<!-- js对象与用户的系统信息，如系统版本（Linux、Mac OS、Windows），浏览器版本（Chrome、Firefox）等 -->
	<script src="../examples/js/libs/system.min.js"></script>

	<!-- 编辑器中的鼠标和触屏交互（时事件处理、内置变量）等 -->
	<!-- <script src="../examples/js/controls/EditorControls.js"></script> -->
	<!-- 物体上的transform标志--translate、rotate、scale，鼠标移入移出等 -->
	<!-- <script src="../examples/js/controls/TransformControls.js"></script> -->

	<!-- 一个用于生成和读取zip文件的JavaScript -->
	<!-- <script src="../examples/js/libs/jszip.min.js"></script> -->
	<!-- ZLIB（RFC 1950），DEFLATE（RFC 1951），GZIP（RFC 1952）和PKZIP实现。 -->
	<!-- <script src="../examples/js/libs/inflate.min.js"></script> -->

	<!-- 三维空间的点到二维屏幕，二维屏幕的uv到三维空间 -->
	<!-- <script src="../examples/js/renderers/Projector.js"></script> -->
	<!-- 通过光线追踪来渲染场景 -->
	<!-- <script src="../examples/js/renderers/RaytracingRenderer.js"></script>
		<script src="../examples/js/renderers/SoftwareRenderer.js"></script>
		<script src="../examples/js/renderers/SVGRenderer.js"></script> -->

	<link rel="stylesheet" href="js/libs/codemirror/codemirror.css">
	<link rel="stylesheet" href="js/libs/codemirror/theme/monokai.css">
	<!-- 在浏览器的DOM之上用JavaScript实现的代码编辑器 -->
	<script src="js/libs/codemirror/codemirror.js"></script>
	<script src="js/libs/codemirror/mode/javascript.js"></script>
	<!-- CodeMirror驱动的GLSL编辑器组件 -->
	<script src="js/libs/codemirror/mode/glsl.js"></script>

	<!-- 通用模块定义（UMD）支持AMD，CommonJS / Node.js Rhino和普通的浏览器加载。 -->
	<script src="js/libs/esprima.js"></script>
	<!-- Json生成解析器 -->
	<script src="js/libs/jsonlint.js"></script>
	<script src="js/libs/glslprep.min.js"></script>

	<!-- 对话框、提示等的框架 -->
	<link rel="stylesheet" href="js/libs/codemirror/addon/dialog.css">
	<link rel="stylesheet" href="js/libs/codemirror/addon/show-hint.css">
	<link rel="stylesheet" href="js/libs/codemirror/addon/tern.css">
	<script src="js/libs/codemirror/addon/dialog.js"></script>
	<script src="js/libs/codemirror/addon/show-hint.js"></script>
	<script src="js/libs/codemirror/addon/tern.js"></script>
	<script src="js/libs/acorn/acorn.js"></script>
	<script src="js/libs/acorn/acorn_loose.js"></script>
	<script src="js/libs/acorn/walk.js"></script>

	<!-- tern.js目录下可实现智能提示补全 -->
	<script src="js/libs/ternjs/polyfill.js"></script>
	<script src="js/libs/ternjs/signal.js"></script>
	<script src="js/libs/ternjs/tern.js"></script>
	<script src="js/libs/ternjs/def.js"></script>
	<script src="js/libs/ternjs/comment.js"></script>
	<script src="js/libs/ternjs/infer.js"></script>
	<script src="js/libs/ternjs/doc_comment.js"></script>
	<script src="js/libs/tern-threejs/threejs.js"></script>

	<script src="js/libs/ui.js"></script>
	<script src="js/libs/ui.three.js"></script>

	<!-- 关于editor的参数初始化设置，如camera的分辨率、窗口大小等 -->
	<script src="js/libs/app.js"></script>
	<!-- play、stop两个选项按键，没有暂停 -->
	<script src="js/Player.js"></script>
	<script src="js/Script.js"></script>

	<!-- 数据初始化、获取、设置等 -->
	<script src="js/Storage.js"></script>

	<!-- editor界面操作响应 -->
	<!-- <script src="js/Editor.js"></script> -->
	<script src="js/panels/Editor.js"></script>
	<!-- 配置文件，如根据key，获取存储的value值   -->
	<script src="js/Config.js"></script>
	<!-- 历史数据存储，如editor的play与stop状态切换，数据也要随之切换 -->
	<script src="js/History.js"></script>
	<!-- 与前面的各种loader目录下的文件对应，调用loader文件 -->
	<script src="js/Loader.js"></script>
	<!-- 总菜单 -->
	<script src="js/Menubar.js"></script>
	<!-- 下面的几个子菜单 -->
	<script src="js/Menubar.File.js"></script>
	<script src="js/Menubar.Edit.js"></script>
	<script src="js/Menubar.Add.js"></script>
	<script src="js/Menubar.Play.js"></script>
	<script src="js/Menubar.Examples.js"></script>
	<script src="js/Menubar.Help.js"></script>
	<script src="js/Menubar.Status.js"></script>

	<!-- 右边工具栏，可对应编辑器界面查看 -->
	<script src="js/Sidebar.js"></script>
	<script src="js/Sidebar.Scene.js"></script>
	<script src="js/Sidebar.Project.js"></script>
	<script src="js/Sidebar.Settings.js"></script>
	<script src="js/Sidebar.Settings.Shortcuts.js"></script>
	<script src="js/Sidebar.Settings.Viewport.js"></script>
	<script src="js/Sidebar.Properties.js"></script>
	<script src="js/Sidebar.Object.js"></script>
	<script src="js/Sidebar.Geometry.js"></script>
	<script src="js/Sidebar.Geometry.Geometry.js"></script>
	<script src="js/Sidebar.Geometry.BufferGeometry.js"></script>
	<script src="js/Sidebar.Geometry.Modifiers.js"></script>
	<script src="js/Sidebar.Geometry.BoxGeometry.js"></script>
	<script src="js/Sidebar.Geometry.CircleGeometry.js"></script>
	<script src="js/Sidebar.Geometry.CylinderGeometry.js"></script>
	<script src="js/Sidebar.Geometry.IcosahedronGeometry.js"></script>
	<script src="js/Sidebar.Geometry.PlaneGeometry.js"></script>
	<script src="js/Sidebar.Geometry.SphereGeometry.js"></script>
	<script src="js/Sidebar.Geometry.TorusGeometry.js"></script>
	<script src="js/Sidebar.Geometry.TorusKnotGeometry.js"></script>

	<!-- 自定义控制面板 -->
	<script src="js/panels/ControlPanel.js"></script>

	<!-- 预定义的经典茶壶模型 -->
	<!-- <script src="../examples/js/geometries/TeapotBufferGeometry.js"></script> -->
	<script src="js/Sidebar.Geometry.TeapotBufferGeometry.js"></script>
	<script src="js/Sidebar.Geometry.LatheGeometry.js"></script>
	<script src="js/Sidebar.Material.js"></script>
	<script src="js/Sidebar.Animation.js"></script>
	<script src="js/Sidebar.Script.js"></script>
	<script src="js/Sidebar.History.js"></script>
	<script src="js/Strings.js"></script>
	<!-- editor左下方的translate、rotate等 -->
	<script src="js/Toolbar.js"></script>

	<!-- <script src="js/Viewport.js"></script> -->
	<script src="js/panels/Viewport.js"></script>
	<script src="js/Viewport.Info.js"></script>

	<!-- commands命令不是特别懂，里面都是execute、undo、update、toJSON、fromJSON等功能 -->
	<script src="js/Command.js"></script>
	<script src="js/commands/AddObjectCommand.js"></script>
	<script src="js/commands/RemoveObjectCommand.js"></script>
	<script src="js/commands/MoveObjectCommand.js"></script>
	<script src="js/commands/SetPositionCommand.js"></script>
	<script src="js/commands/SetRotationCommand.js"></script>
	<script src="js/commands/SetScaleCommand.js"></script>
	<script src="js/commands/SetValueCommand.js"></script>
	<script src="js/commands/SetUuidCommand.js"></script>
	<script src="js/commands/SetColorCommand.js"></script>
	<script src="js/commands/SetGeometryCommand.js"></script>
	<script src="js/commands/SetGeometryValueCommand.js"></script>
	<script src="js/commands/MultiCmdsCommand.js"></script>
	<script src="js/commands/AddScriptCommand.js"></script>
	<script src="js/commands/RemoveScriptCommand.js"></script>
	<script src="js/commands/SetScriptValueCommand.js"></script>
	<script src="js/commands/SetMaterialCommand.js"></script>
	<script src="js/commands/SetMaterialValueCommand.js"></script>
	<script src="js/commands/SetMaterialColorCommand.js"></script>
	<script src="js/commands/SetMaterialMapCommand.js"></script>
	<script src="js/commands/SetSceneCommand.js"></script>

	<script src="js/libs/html2canvas.js"></script>
	<script src="js/libs/three.html.js"></script>

	<script>

		window.URL = window.URL || window.webkitURL;
		window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;

		Number.prototype.format = function () {
			return this.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
		};

		// 设置editor的一些初始状态，进行一个总的调用
		var editor = new Editor();

		var viewport = new Viewport(editor);
		document.body.appendChild(viewport.dom);

		// var toolbar = new Toolbar(editor);
		// document.body.appendChild(toolbar.dom);

		// var script = new Script(editor);
		// document.body.appendChild(script.dom);

		// var player = new Player(editor);
		// document.body.appendChild(player.dom);

		var menubar = new Menubar(editor);
		document.body.appendChild(menubar.dom);

		// var sidebar = new Sidebar(editor);
		// document.body.appendChild(sidebar.dom);

		var controlPanel = new ControlPanel();
		document.body.appendChild(controlPanel.dom);

		// var modal = new UI.Modal();
		// document.body.appendChild(modal.dom);

		editor.setTheme(editor.config.getKey('theme'));

		editor.storage.init(function () {

			editor.storage.get(function (state) {

				if (isLoadingFromHash) return;

				if (state !== undefined) {

					editor.fromJSON(state);

				}

				var selected = editor.config.getKey('selected');

				if (selected !== undefined) {

					editor.selectByUuid(selected);

				}

			});

			//

			var timeout;

			function saveState(scene) {

				if (editor.config.getKey('autosave') === false) {

					return;

				}

				clearTimeout(timeout);

				timeout = setTimeout(function () {

					editor.signals.savingStarted.dispatch();

					timeout = setTimeout(function () {

						editor.storage.set(editor.toJSON());

						editor.signals.savingFinished.dispatch();

					}, 100);

				}, 1000);

			};

			var signals = editor.signals;

			signals.geometryChanged.add(saveState);
			signals.objectAdded.add(saveState);
			signals.objectChanged.add(saveState);
			signals.objectRemoved.add(saveState);
			signals.materialChanged.add(saveState);
			signals.sceneBackgroundChanged.add(saveState);
			signals.sceneFogChanged.add(saveState);
			signals.sceneGraphChanged.add(saveState);
			signals.scriptChanged.add(saveState);
			signals.historyChanged.add(saveState);

			signals.showModal.add(function (content) {

				modal.show(content);

			});

		});

		//

		document.addEventListener('dragover', function (event) {

			event.preventDefault();
			event.dataTransfer.dropEffect = 'copy';

		}, false);

		document.addEventListener('drop', function (event) {

			event.preventDefault();

			editor.loader.loadFiles(event.dataTransfer.files);

		}, false);

		function onWindowResize(event) {

			editor.signals.windowResize.dispatch();

		}

		window.addEventListener('resize', onWindowResize, false);

		onWindowResize();

		//

		var isLoadingFromHash = false;
		var hash = window.location.hash;

		if (hash.substr(1, 5) === 'file=') {

			var file = hash.substr(6);

			if (confirm('Any unsaved data will be lost. Are you sure?')) {

				var loader = new THREE.FileLoader();
				loader.crossOrigin = '';
				loader.load(file, function (text) {

					editor.clear();
					editor.fromJSON(JSON.parse(text));

				});

				isLoadingFromHash = true;

			}

		}
		
		/*
		window.addEventListener( 'message', function ( event ) {

			editor.clear();
			editor.fromJSON( event.data );

		}, false );
		*/

	</script>
</body>

</html>